package net.caixiaomi.info.ui.dialog;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.ElementCellAdapter;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseDialogFragment;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.model.CommonEvent;
import net.caixiaomi.info.model.ElementCellModel;
import net.caixiaomi.info.model.MatchPlaysList;
import net.caixiaomi.info.model.PlayFootballItem;
import net.caixiaomi.info.util.EventWhat;
import net.caixiaomi.info.widgets.GridDivider;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 混合过关dialog
 */

public class BlendDialogFragment extends BaseDialogFragment implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.host)
    TextView mHost;
    @BindView(R.id.guest)
    TextView mGuest;

    @BindView(R.id.color1)
    TextView mColor1;
    @BindView(R.id.color2)
    TextView mColor2;
    @BindView(R.id.color3)
    TextView mColor3;
    @BindView(R.id.color4)
    TextView mColor4;
    @BindView(R.id.color5)
    TextView mColor5;

    @BindView(R.id.list1)
    RecyclerView mListView1;
    @BindView(R.id.list2)
    RecyclerView mListView2;
    @BindView(R.id.list3)
    RecyclerView mListView3;
    @BindView(R.id.list4)
    RecyclerView mListView4;
    @BindView(R.id.list5)
    RecyclerView mListView5;
    @BindView(R.id.list6)
    RecyclerView mListView6;
    @BindView(R.id.list7)
    RecyclerView mListView7;


    @BindView(R.id.container)
    NestedScrollView mScrollView;

    private PlayFootballItem mPlayFootBallItem;
    private ElementCellAdapter mAdapter1;
    private ElementCellAdapter mAdapter2;
    private ElementCellAdapter mAdapter3;
    private ElementCellAdapter mAdapter4;
    private ElementCellAdapter mAdapter5;
    private ElementCellAdapter mAdapter6;
    private ElementCellAdapter mAdapter7;

    @OnClick(android.R.id.button1)
    void toConfirm() {
        try {
            String text = JSON.toJSONString(mPlayFootBallItem);
            EventBus.getDefault().post(new CommonEvent(EventWhat.EVENT_FOOTBALL_SELECT_DONE, text));
            dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick(android.R.id.button2)
    void toCancel() {
        dismiss();
    }

    @Override
    protected int layoutRes() {
        return R.layout.dialog_blend;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            String strData = getArguments().getString("data");
            mPlayFootBallItem = JSON.parseObject(strData, new TypeReference<PlayFootballItem>() {
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onViewCreated(@NonNull View view) {
        try {
            Window window = getDialog().getWindow();
            WindowManager.LayoutParams wlp = window.getAttributes();
            wlp.width = ViewGroup.LayoutParams.MATCH_PARENT;
            wlp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            wlp.gravity = Gravity.BOTTOM;
            window.setAttributes(wlp);
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            window.setBackgroundDrawable(new ColorDrawable(0xff000000));
            getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);

            ViewGroup.LayoutParams layoutParams = mScrollView.getLayoutParams();
            layoutParams.height = AppHelper.getScreenHeight(CommonApp.getInstance()) / 3 * 2;

            StringBuilder stringBuilder = new StringBuilder();
            mHost.setText(mPlayFootBallItem.getHomeTeamAbbr());
            mGuest.setText(mPlayFootBallItem.getVisitingTeamAbbr());


            if (mPlayFootBallItem.getSelectCell() != null) {
                mPlayFootBallItem.getSelectCell().clear();
            }
            String title = "";
            List<ElementCellModel> cellModelList = new ArrayList<>();
            MatchPlaysList matchPlaysList = mPlayFootBallItem.getMatchPlays().get(0);//让球胜平负
            cellModelList.add(matchPlaysList.getHomeCell());
            cellModelList.add(matchPlaysList.getFlatCell());
            cellModelList.add(matchPlaysList.getVisitingCell());
            title = getString(R.string.C_VICTORY_AND_DEFEAT_1).substring(0, 2);
            for (int index = 0; index < title.length(); index++) {
                stringBuilder.append(title.charAt(index)).append("\n");
            }
            stringBuilder.append(matchPlaysList.getFixedOdds());

            for (ElementCellModel cellModel : cellModelList) {
                cellModel.setPlayType(matchPlaysList.getPlayType());
                if (cellModel.isSelected()) {
                    mPlayFootBallItem.getSelectCell().add(cellModel);
                }
            }

            mColor2.setText(stringBuilder);
            mListView2.setLayoutManager(new GridLayoutManager(getActivity(), 3));
            mListView2.addItemDecoration(new GridDivider(getActivity(), (int) getResources().getDimension(R.dimen.divider),  ContextCompat.getColor(getActivity(),R.color.light_gray)));
            mListView2.setNestedScrollingEnabled(false);
            mAdapter2 = new ElementCellAdapter(R.layout.view_textview);
            mAdapter2.setOnItemClickListener(this);
            mAdapter2.setNewData(cellModelList);
            mAdapter2.bindToRecyclerView(mListView2);

            matchPlaysList = mPlayFootBallItem.getMatchPlays().get(1);//胜平负
            cellModelList = new ArrayList<>();
            if (TextUtils.equals(matchPlaysList.getShow(), "1")) {
                cellModelList.add(matchPlaysList.getHomeCell());
                cellModelList.add(matchPlaysList.getFlatCell());
                cellModelList.add(matchPlaysList.getVisitingCell());
                for (ElementCellModel cellModel : cellModelList) {
                    cellModel.setPlayType(matchPlaysList.getPlayType());
                    if (cellModel.isSelected()) {
                        mPlayFootBallItem.getSelectCell().add(cellModel);
                    }
                }
            } else {
                ElementCellModel noOpen = new ElementCellModel();
                noOpen.setCellName(getString(R.string.C_NO_OPEN));
                noOpen.setCellOdds("0.00");
                cellModelList.add(noOpen);
            }

            title = getTitle(getString(R.string.C_VICTORY_AND_DEFEAT));
            mColor1.setText(title);
            mListView1.setLayoutManager(new GridLayoutManager(getActivity(), 3));
            mListView1.setNestedScrollingEnabled(false);
            mListView1.addItemDecoration(new GridDivider(getActivity(), (int) getResources().getDimension(R.dimen.divider),  ContextCompat.getColor(getActivity(),R.color.light_gray)));
            mAdapter1 = new ElementCellAdapter(R.layout.view_textview);
            if (TextUtils.equals(matchPlaysList.getShow(), "1")) {
                mAdapter1.setOnItemClickListener(this);
            }
            mAdapter1.setNewData(cellModelList);
            mAdapter1.bindToRecyclerView(mListView1);

            matchPlaysList = mPlayFootBallItem.getMatchPlays().get(2);//比分
            cellModelList = matchPlaysList.getHomeCell().getCellSons();
            title = getTitle(getString(R.string.C_SCORE));
            mColor3.setText(title);
            mListView3.setLayoutManager(new GridLayoutManager(getActivity(), 5));
            mListView3.addItemDecoration(new GridDivider(getActivity(), (int) getResources().getDimension(R.dimen.divider),  ContextCompat.getColor(getActivity(),R.color.light_gray)));
            mListView3.setNestedScrollingEnabled(false);
            mAdapter3 = new ElementCellAdapter(R.layout.view_textview);
            mAdapter3.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
                @Override
                public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                    if(position == mAdapter3.getData().size() -1)
                        return 3;
                    return 1;
                }
            });
            mAdapter3.setOnItemClickListener(this);
            mAdapter3.setNewData(cellModelList);
            mListView3.setAdapter(mAdapter3);
            mAdapter3.bindToRecyclerView(mListView3);
            for (ElementCellModel cellModel : cellModelList) {
                cellModel.setPlayType(matchPlaysList.getPlayType());
                if (cellModel.isSelected()) {
                    mPlayFootBallItem.getSelectCell().add(cellModel);
                }
            }

            cellModelList = matchPlaysList.getFlatCell().getCellSons();
            mListView4.setLayoutManager(new GridLayoutManager(getActivity(), 5));
            mListView4.addItemDecoration(new GridDivider(getActivity(), (int) getResources().getDimension(R.dimen.divider),  ContextCompat.getColor(getActivity(),R.color.light_gray)));
            mListView4.setNestedScrollingEnabled(false);
            mAdapter4 = new ElementCellAdapter(R.layout.view_textview);
            mAdapter4.setOnItemClickListener(this);
            mAdapter4.setNewData(cellModelList);
            mListView4.setAdapter(mAdapter4);
            mAdapter4.bindToRecyclerView(mListView4);
            for (ElementCellModel cellModel : cellModelList) {
                cellModel.setPlayType(matchPlaysList.getPlayType());
                if (cellModel.isSelected()) {
                    mPlayFootBallItem.getSelectCell().add(cellModel);
                }
            }

            cellModelList = matchPlaysList.getVisitingCell().getCellSons();
            mListView5.setLayoutManager(new GridLayoutManager(getActivity(), 5));
            mListView5.addItemDecoration(new GridDivider(getActivity(), (int) getResources().getDimension(R.dimen.divider),  ContextCompat.getColor(getActivity(),R.color.light_gray)));
            mListView5.setNestedScrollingEnabled(false);
            mAdapter5 = new ElementCellAdapter(R.layout.view_textview);
            mAdapter5.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
                @Override
                public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                    if(position == mAdapter5.getData().size() -1)
                        return 3;
                    return 1;
                }
            });
            mAdapter5.setOnItemClickListener(this);
            mAdapter5.setNewData(cellModelList);
            mListView5.setAdapter(mAdapter5);
            mAdapter5.bindToRecyclerView(mListView5);
            for (ElementCellModel cellModel : cellModelList) {
                cellModel.setPlayType(matchPlaysList.getPlayType());
                if (cellModel.isSelected()) {
                    mPlayFootBallItem.getSelectCell().add(cellModel);
                }
            }

            matchPlaysList = mPlayFootBallItem.getMatchPlays().get(3);//总进球
            title = getTitle(getString(R.string.C_ALL_GOALS));
            cellModelList = matchPlaysList.getMatchCells();
            mColor4.setText(title);
            mListView6.setLayoutManager(new GridLayoutManager(getActivity(), 8));
            mListView6.addItemDecoration(new GridDivider(getActivity(), (int) getResources().getDimension(R.dimen.divider),  ContextCompat.getColor(getActivity(),R.color.light_gray)));
            mListView6.setNestedScrollingEnabled(false);
            mAdapter6 = new ElementCellAdapter(R.layout.view_textview);
            mAdapter6.setOnItemClickListener(this);
            mAdapter6.setNewData(cellModelList);
            mListView6.setAdapter(mAdapter6);
            mAdapter6.bindToRecyclerView(mListView6);
            for (ElementCellModel cellModel : cellModelList) {
                cellModel.setPlayType(matchPlaysList.getPlayType());
                if (cellModel.isSelected()) {
                    mPlayFootBallItem.getSelectCell().add(cellModel);
                }
            }

            matchPlaysList = mPlayFootBallItem.getMatchPlays().get(4);//半全场
            title = getTitle(getString(R.string.C_HALF_AND_ALL));
            cellModelList = matchPlaysList.getMatchCells();
            mColor5.setText(title);
            mListView7.setLayoutManager(new GridLayoutManager(getActivity(), 3));
            mListView7.addItemDecoration(new GridDivider(getActivity(), (int) getResources().getDimension(R.dimen.divider),  ContextCompat.getColor(getActivity(),R.color.light_gray)));
            mListView7.setNestedScrollingEnabled(false);
            mAdapter7 = new ElementCellAdapter(R.layout.view_textview);
            mAdapter7.setOnItemClickListener(this);
            mAdapter7.setNewData(cellModelList);
            mListView7.setAdapter(mAdapter7);
            mAdapter7.bindToRecyclerView(mListView7);
            for (ElementCellModel cellModel : cellModelList) {
                cellModel.setPlayType(matchPlaysList.getPlayType());
                if (cellModel.isSelected()) {
                    cellModel.setPlayType(matchPlaysList.getPlayType());
                    mPlayFootBallItem.getSelectCell().add(cellModel);
                }
            }

            View parent = (View) view.getParent();

//            BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
//            view.measure(0, 0);
//            behavior.setPeekHeight(view.getMeasuredHeight());

            getDialog().setCanceledOnTouchOutside(true);
            getDialog().setCancelable(true);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getTitle(String title) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int index = 0; index < title.length(); index++) {
            stringBuilder.append(title.charAt(index));
            if (index != title.length() - 1) {
                stringBuilder.append("\n");
            }
        }
        return stringBuilder.toString();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        try {
            if (mPlayFootBallItem.getSelectCell() == null) {
                mPlayFootBallItem.setSelectCell(new ArrayList<ElementCellModel>());
            }
            ElementCellModel cellModel = (ElementCellModel) adapter.getData().get(position);
            cellModel.setSelected(!cellModel.isSelected());
            if (cellModel.isSelected()) {
                mPlayFootBallItem.getSelectCell().add(cellModel);
            } else {
                mPlayFootBallItem.getSelectCell().remove(cellModel);
            }
            adapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPlayFootBallItem = null;
    }
}
